package com.dycn.util;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;
@Slf4j
public class OkhttpUtil {
//    public   static  final String APPID="wxaa974928147c87b9";
//    private  static  final String APPSECRET="dc103b7fa2497aba84f1aa24882201de";

    public   static  final String APPID="wxb3174ce9c434e763";
    private  static  final String APPSECRET="fe32c9bb5cc4cd2a257251d8b5e5f6e8";
//https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxaa974928147c87b9&redirect_uri=http%3a%2f%2fwx.sxbq998.cn/tasks/list&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect
    static OkHttpClient okHttpClient;
    static  OkhttpUtil juheUtil;
    private  OkhttpUtil(){}
    public   static  OkhttpUtil getInstance(){
        if(okHttpClient==null){
            synchronized (OkhttpUtil.class){
                if(okHttpClient==null){
                    okHttpClient=new OkHttpClient();
                    juheUtil=new OkhttpUtil();
                }
            }
        }
        return  juheUtil;
    }

        public   String getUrl(HttpServletRequest request){
            String url = "";
            url = request.getScheme() +"://" + request.getServerName()
                    + ":" +request.getServerPort()+"/";
            //+ request.getServletPath();
            if(request.getServerPort()==80){
                url = request.getScheme() +"://" + request.getServerName()+"/";
                //+ request.getServletPath();
            }
//        if (request.getQueryString() != null){
//            url += "?" + request.getQueryString();
//        }
            return  url;
        }

    public  String get(String url){
        Request request=new Request.Builder().url(url).build();
        Call call=okHttpClient.newCall(request);
        try {
            Response response=call.execute();
            return  response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
            return  null;
        }
    }
    public  String post(String url, Map<String,String> map){
        FormBody.Builder builder=new FormBody.Builder();
        if(map!=null){
            for (String key:map.keySet()){
                builder.add(key,map.get(key));
            }
        }
        FormBody formBody=builder.build();
        Request request=new Request.Builder().url(url).post(formBody).build();
        Call call=okHttpClient.newCall(request);
        try {
            Response response=call.execute();
            return  response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
            return  null;
        }
    }

    public  String getAccessToken(){
        //{"errcode":40164,"errmsg":"invalid ip 110.179.94.81, not in whitelist hint: [T1sFWa0198sha2]"}
        String url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+APPID+"&secret="+APPSECRET;
        String response=get(url);
        JsonObject jsonObject=new Gson().fromJson(response,JsonObject.class);
        return  jsonObject.get("access_token").getAsString();
    }

    public  String getTicket(String token){
        String url="https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+token+"&type=jsapi";
        String response=get(url);
        JsonObject jsonObject=new Gson().fromJson(response,JsonObject.class);
        return  jsonObject.get("ticket").getAsString();
    }


    public  String getWeixinAccess(String code){
        String url="https://api.weixin.qq.com/sns/oauth2/access_token?appid="+APPID+"&secret="+APPSECRET+"&code="+code+"&grant_type=authorization_code";
        String response=get(url);
        log.debug(code);
        log.debug(response);
       // response="{\"access_token\":\"13_8S9O_OnwNdofY-hudW7UolpV2wqSMXLKYLb7AYxqv-G-J1Qe7xhEYzHt-yBURfKTESeMn3Cxl_xj5E7XbNMJ4tDnJ9TdSdAOiE2rIfXPCNs\",\"expires_in\":7200,\"refresh_token\":\"13_BQxj3dyB_eJQyIzNWh-WlHNUzTGCLtP_SKDi_Ia5MNfr567n8PsNklHZWtH-JWfmgfnPdBYN_mzss7MI-ac33e0Y9pKgcXUE7Armg12Ju8c\",\"openid\":\"o9XqEwoibgA2_D3fLBVQ7LV6h8uY\",\"scope\":\"snsapi_userinfo\"}";
        JsonObject jsonObject=new Gson().fromJson(response,JsonObject.class);
        String token=jsonObject.get("access_token").getAsString();
        String openId=jsonObject.get("openid").getAsString();
        url="https://api.weixin.qq.com/sns/userinfo?access_token="+token+"&openid="+openId+"&lang=zh_CN";
        return  get(url);
    }
}
